package com.lib.application;

import com.lib.dto.RentalAgentQRCodeDTO;
import com.lib.model.RentalAgent;
import com.lib.model.RentalAgentQRCode;
import com.lib.model.ShopQRCode;
import com.lib.model.WxAccount;
import com.lib.repository.RentalAgentQRCodeRepository;
import com.lib.repository.RentalAgentRepository;
import com.lib.repository.ShopQRCodeRepository;
import com.lib.repository.WxAccountReponsitory;
import com.lib.service.WxCustomerService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalTime;
import java.util.Date;

/**
 * Created by duqingxiang on 17/5/24.
 */
@Service
public class RentalAgentQRCodeService {

    @Autowired
    WxCustomerService wxCustomerService;
    @Autowired
    WxAccountReponsitory wxAccountReponsitory;
    @Autowired
    RentalAgentRepository rentalAgentRepository;

    @Autowired
    RentalAgentQRCodeRepository rentalAgentQRCodeRepository;

    public String getMyCode(String openId){


        WxAccount wxAccount = wxAccountReponsitory.findWxAccountByOpenId(openId);
        if (wxAccount == null) {
            return null;
        }

        RentalAgent agent = wxAccount.getAgent();
        if (agent == null) {
            return null;
        }


        RentalAgentQRCode code = rentalAgentQRCodeRepository.findRentalAgentQRCodeByOpenId(openId);

        if (code != null) {
            long intervalSeconds = (System.currentTimeMillis() - code.getModifyDate().getTime())/1000;
            if (intervalSeconds < code.getExpireSeconds()) {
                return code.getTicket();
            }
        } else  {
            code = RentalAgentQRCode.builder()
                    .openId(openId)
                    .agentId(agent.getId())
                    .modifyDate(new Date())
                    .createdDate(new Date())
                    .build();
            code = rentalAgentQRCodeRepository.saveAndFlush(code);
        }



        try {
            //经纪人二维码生成  其实是在租客的公众号上生成的，扫码也是针对租客用的
            WxMpQrCodeTicket ticket = wxCustomerService.getQrcodeService().qrCodeCreateTmpTicket(code.getId(),RentalAgentQRCode.MAX_TMP_EXPIRE_SECONDS);

            code.setExpireSeconds(ticket.getExpire_seconds());
            code.setTicket(ticket.getTicket());
            code.setUrl(ticket.getUrl());
            code.setModifyDate(new Date());

            code = rentalAgentQRCodeRepository.saveAndFlush(code);

            return code.getTicket();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public RentalAgentQRCodeDTO getRentalAgentQRCodeDTO(int id) {
        RentalAgentQRCode code = rentalAgentQRCodeRepository.findById(id);
        if (code == null)
            return null;

        RentalAgent agent = rentalAgentRepository.findOne(code.getAgentId());
        if (agent == null)
            return null;


        RentalAgentQRCodeDTO dto = RentalAgentQRCodeDTO.builder()
                .agent(agent)
                .qrCode(code)
                .build();


        return dto;
    }


}
